/*
 * TestLayout.java
 *
 * Created on 2012-03-14, 11:41:33
 */
package czat;

import java.awt.EventQueue;
import javax.swing.DefaultListModel;

/**
 *
 * @author zztbak01
 */
public class ChatClient extends javax.swing.JFrame {

	private static final long serialVersionUID = 0L;
	private DefaultListModel listModel = new DefaultListModel();
	private WatekKlienta watekKlienta;
	public String login;

	/** Creates new form TestLayout */
	public ChatClient(String login, WatekKlienta watek) {
		super("Login: " + login);
		this.login = login;
		initComponents();
		watekKlienta = watek;
		listModel.addElement("ALL");
		usersList.setModel(listModel);
	}

	public void addActiveUser(final String user) {
		EventQueue.invokeLater(new Thread() {

			@Override
			public void run() {
				listModel.addElement(user);
			}
		});
	}

	public void removeActiveUser(final String user) {
		EventQueue.invokeLater(new Thread() {

			@Override
			public void run() {
				listModel.removeElement(user);
			}
		});
	}

	public void addText(String from, String to, String text) {
		if (!text.endsWith("\n")) {
			text += "\n";
		}
		final String result = from + " -> " + to + ": " + text;
		EventQueue.invokeLater(new Thread() {

			@Override
			public void run() {
				chatArea.append(result);
			}
		});
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {
    java.awt.GridBagConstraints gridBagConstraints;

    jSplitPane1 = new javax.swing.JSplitPane();
    jSplitPane2 = new javax.swing.JSplitPane();
    jScrollPane1 = new javax.swing.JScrollPane();
    chatArea = new javax.swing.JTextArea();
    jScrollPane3 = new javax.swing.JScrollPane();
    messageArea = new javax.swing.JTextArea();
    jScrollPane2 = new javax.swing.JScrollPane();
    usersList = new javax.swing.JList();
    sendButton = new javax.swing.JButton();
    closeButton = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setMinimumSize(new java.awt.Dimension(400, 400));
    getContentPane().setLayout(new java.awt.GridBagLayout());

    jSplitPane1.setResizeWeight(0.8);
    jSplitPane1.setPreferredSize(new java.awt.Dimension(429, 396));

    jSplitPane2.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
    jSplitPane2.setResizeWeight(0.8);
    jSplitPane2.setMinimumSize(new java.awt.Dimension(70, 100));

    jScrollPane1.setPreferredSize(new java.awt.Dimension(350, 250));

    chatArea.setColumns(20);
    chatArea.setEditable(false);
    chatArea.setRows(5);
    chatArea.setPreferredSize(new java.awt.Dimension(330, 230));
    jScrollPane1.setViewportView(chatArea);

    jSplitPane2.setTopComponent(jScrollPane1);

    jScrollPane3.setPreferredSize(new java.awt.Dimension(350, 100));

    messageArea.setColumns(20);
    messageArea.setLineWrap(true);
    messageArea.setRows(5);
    messageArea.setPreferredSize(new java.awt.Dimension(330, 90));
    jScrollPane3.setViewportView(messageArea);

    jSplitPane2.setRightComponent(jScrollPane3);

    jSplitPane1.setLeftComponent(jSplitPane2);

    usersList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
    usersList.setPreferredSize(new java.awt.Dimension(50, 300));
    jScrollPane2.setViewportView(usersList);

    jSplitPane1.setRightComponent(jScrollPane2);

    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridwidth = 2;
    gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
    gridBagConstraints.weightx = 1.0;
    gridBagConstraints.weighty = 0.9;
    getContentPane().add(jSplitPane1, gridBagConstraints);

    sendButton.setText("Wyślij");
    sendButton.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        sendButtonActionPerformed(evt);
      }
    });
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 1;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
    gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 0);
    getContentPane().add(sendButton, gridBagConstraints);

    closeButton.setText("Zamknij");
    closeButton.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        closeButtonActionPerformed(evt);
      }
    });
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 1;
    gridBagConstraints.gridy = 1;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
    gridBagConstraints.insets = new java.awt.Insets(5, 0, 5, 5);
    getContentPane().add(closeButton, gridBagConstraints);

    pack();
  }// </editor-fold>//GEN-END:initComponents

		private void sendButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sendButtonActionPerformed
			Object sendTo = usersList.getSelectedValue();
			String text = messageArea.getText();
			if (sendTo != null && text != null && !"".equals(text)) {
				watekKlienta.wyslij("TEXT;" + sendTo + ";" + text);
				messageArea.setText(null);
				addText(login, sendTo + "", text);
			}
		}//GEN-LAST:event_sendButtonActionPerformed

		private void closeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeButtonActionPerformed
			watekKlienta.wyslij("LOGOUT");
			System.exit(0);
		}//GEN-LAST:event_closeButtonActionPerformed
  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JTextArea chatArea;
  private javax.swing.JButton closeButton;
  private javax.swing.JScrollPane jScrollPane1;
  private javax.swing.JScrollPane jScrollPane2;
  private javax.swing.JScrollPane jScrollPane3;
  private javax.swing.JSplitPane jSplitPane1;
  private javax.swing.JSplitPane jSplitPane2;
  private javax.swing.JTextArea messageArea;
  private javax.swing.JButton sendButton;
  private javax.swing.JList usersList;
  // End of variables declaration//GEN-END:variables
}
